package com.zxl.live.tools.guide;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

/**
 * 基础权限验证
 * Created by Administrator on 2016/10/26.
 */
public abstract class BasePermissionGuide implements Runnable {

    private boolean mIsRunning = false;

    protected Context mContext;

    protected Class mStartActivityCls;

    public BasePermissionGuide(Context mContext, Class mStartActivityCls) {
        this.mContext = mContext;
        this.mStartActivityCls = mStartActivityCls;
    }

    /**
     * 启动数据监听
     */
    public void startMonitor() {
        if (!mIsRunning) {
            mIsRunning = true;
            new Thread(this).start();
        }
    }

    /**
     * 停止数据监听
     */
    public void stopMonitor() {
        mIsRunning = false;
    }

    @Override
    public void run() {
        while (mIsRunning) {
            if (havePermission()) {
                mIsRunning = false;
                if (null != mContext) {
                    Intent intent = new Intent(mContext, mStartActivityCls);
                    if (!(mContext instanceof Activity)) {
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    }
                    mContext.startActivity(intent);
                }
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 是否已经获取到权限
     * @return
     */
    public abstract boolean havePermission();
}
